package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;



    @Override
    public void add(AddressBook addressBook) {

        Long userId = BaseContext.getCurrentId();
        //新建地址 补充属性  判断如果是第一次设置 设置是默认地址
        addressBook.setUserId(userId);

        Integer num = addressBookMapper.countByUserId(userId);

        if( num > 0 ){
            //不是第一次添加
            addressBook.setIsDefault(0);

        }else {
            //第一次添加，设置为默认
            addressBook.setIsDefault(1);
        }

        addressBookMapper.insert(addressBook);


    }

    @Override
    public List<AddressBook> listByUserId(Long userId) {

        List<AddressBook> addressBookList = addressBookMapper.listByUserId(userId);


        return addressBookList;
    }

    /**
     * 查看默认地址
     * @param userId
     * @return
     */
    @Override
    public AddressBook selectDefault(Long userId) {
        Integer is_default = 1;
        AddressBook addressBook = addressBookMapper.selectDefault(userId , is_default);

        return addressBook;
    }

    /**
     * 更新地址
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookMapper.updateById(addressBook);


    }

    @Override
    public void deleteById(Long id) {

        addressBookMapper.deleteById(id);



    }

    @Override
    public AddressBook selectById(Long id) {

        AddressBook addressBook = addressBookMapper.selectById(id);
        return addressBook;
    }

    /**
     * 根据id设置默认地址
     * @param addressBook
     */
    @Override
    @Transactional
    public void setDefalut(AddressBook addressBook) {
        //将现在有的默认地址改为0
        AddressBook selectDefault = addressBookMapper.selectDefault(BaseContext.getCurrentId(), StatusConstant.ENABLE);
        //添加条件判断 可能会出现 默认地址为0 的情况
        if (selectDefault != null) {
            selectDefault.setIsDefault(StatusConstant.DISABLE);
            addressBookMapper.updateById(selectDefault);
            //将id的地址设为1
            AddressBook addressBook1 = new AddressBook();
            BeanUtils.copyProperties(selectDefault, addressBook1);
            //通过新的id进行设置
            addressBook1.setIsDefault(StatusConstant.ENABLE);

            //
            addressBook1.setId(addressBook.getId());
            addressBookMapper.updateById(addressBook1);
        }else{

            addressBook.setIsDefault(StatusConstant.ENABLE);
            addressBookMapper.updateById(addressBook);
        }



    }
}